package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.CollectComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.ICollectService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private ICollectService collectService;

    //保存收藏
    @PostMapping
    @ResponseBody
    public Object save(Collect collect){
        collect.setUserId(UserContext.getCurrentUser().getId());
        collectService.save(collect);
        return null;
    }

    //查询个人所有收藏
    @GetMapping
    @ResponseBody
    public Object list(){
        User user = UserContext.getCurrentUser();
        Map<String, Object> map = collectService.list(user.getId());
        return map;
    }

    //查询当前用户的某个收藏
    @GetMapping("/select")
    @ResponseBody
    public Object select(Collect collect){
        JSONResult result = new JSONResult();
        collect.setUserId(UserContext.getCurrentUser().getId());
        if(collectService.select(collect)==null){
            result.mark("已收藏");
        }
        return result;
    }


    //查询某篇文章的评论
    @GetMapping("/listComment")
    @ResponseBody
    public Object listComment(CollectComment collectComment){

        return  collectService.listComment(collectComment);
    }


    //保存某篇文章的评论
    @PostMapping("/listComment")
    @ResponseBody
    public Object saveComment(CollectComment collectComment){
        JSONResult result = new JSONResult();
        if(collectComment.getContent().length()==0){
            result.mark("不能不写");
            return result;
        }
        try {
            collectComment.setUser(UserContext.getCurrentUser());
            collectService.saveComment(collectComment);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("评论失败");
        }
        return result;
    }

}
